<?php
namespace app\admin\model;

use think\Model;

/**
 * 轮播图模型
 * @package app\admin\model
 */
class GwBananer extends Model
{

    public static function getList($request)
    {
        $data = self::alias('b')
            ->field('b.*,c.catname,c.catdir')
            ->leftjoin('gw_cate c', 'c.id = b.cate_id')
            ->paginate($request['limit'], false, ['query' => $request])
            ->each(function ($item, $key) {
                switch ($item->status) {
                    case '0':
                        $item->status = '<span class="layui-badge status-jy">已禁用</span>';
                        break;
                    case '1':
                        $item->status = '<span class="layui-badge layui-bg-green status-qy">已启用</span>';
                        break;
                    default:
                        $item->status = '<span class="layui-badge layui-bg-orange status-jy">未知</span>';
                        break;
                }
                // $item->image_url = "<img src='" . $item->image . "' alt='' />";
                $item->open = '<a href="javascript:;" style="color: #01aaed;" onClick="open_window(\'查看\',\'' . $item->image . '\')"><u>查看</u></a>';

            });

        return $data->toarray();
    }

    public static function add($data)
    {
        try {
            self::create($data);
            return ['code' => '200', 'msg' => '提交成功'];
        } catch (\Exception $e) {

            return ['code' => '422', 'msg' => '提交失败'];
        }
    }

    public static function upd($data)
    {
        try {
            self::where('id', $data['id'])->update($data);
            return ['code' => '200', 'msg' => '更新成功'];
        } catch (\Exception $e) {

            return ['code' => '422', 'msg' => '更新失败'];
        }
    }
}
